package com.yuexun.dhdoctor.module.email.presenter.impl;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.email.bean.UploadBean;
import com.yuexun.dhdoctor.module.email.model.IUploadModel;
import com.yuexun.dhdoctor.module.email.presenter.IUploadPresenter;
import com.yuexun.dhdoctor.module.email.ui.view.IUploadView;

import org.kymjs.kjframe.ui.ViewInject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by work-jx on 2016/8/29.
 */
public class UploadPresenter implements IUploadPresenter {
    private IUploadView view;
    private IUploadModel model;

    public UploadPresenter(IUploadModel model, IUploadView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void uploadFile(String path) {
        model.uploadFile(path)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        view.uploadDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("jx", e.toString());
                        ViewInject.toast("上传失败");
                        view.uploadDialogStatus(AppConfig.DISMISS_DIALOG);
                    }

                    @Override
                    public void onNext(String string) {
                        Log.e("jx", string);
                        UploadBean uploadBean = JSON.parseObject(string, UploadBean.class);
                        view.uploadDialogStatus(AppConfig.DISMISS_DIALOG);
                        view.updateFile(uploadBean);
                    }
                });
    }
}
